package com.ruoyi.commodity.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.commodity.service.IPaymentService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.PaymentMethod;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.commodity.mapper.PaymentMethodMapper;

/**
 * 供应商管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-18
 */
@Service
public class PaymentServiceImpl implements IPaymentService
{
    @Autowired
    private PaymentMethodMapper paymentMapper;

    /**
     * 查询供应商管理
     * 
     * @param id 供应商管理主键
     * @return 供应商管理
     */
    @Override
    public PaymentMethod selectPaymentById(Long id)
    {
        return paymentMapper.selectPaymentById(id);
    }

    /**
     * 查询供应商管理列表
     * 
     * @param payment 供应商管理
     * @return 供应商管理
     */
    @Override
    @DataScope(paymentAlias = "p")
    public List<PaymentMethod> selectPaymentList(PaymentMethod payment)
    {
        return paymentMapper.selectPaymentList(payment);
    }

    @Override
    public List<TreeSelect> selectPaymentTreeList(PaymentMethod payment) {
        List<PaymentMethod> paymentList = SpringUtils.getAopProxy(this).selectPaymentList(payment);
        return buildPaymentTreeSelect(paymentList);
    }

    @Override
    public List<PaymentMethod> buildPaymentTree(List<PaymentMethod> paymentList) {
        List<PaymentMethod> returnList = new ArrayList<>();
        List<Long> collect = paymentList.stream().map(PaymentMethod::getId).collect(Collectors.toList());
        for (PaymentMethod payment:paymentList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(payment.getParentId())){
                recursionFn(paymentList,payment);
                returnList.add(payment);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = paymentList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildPaymentTreeSelect(List<PaymentMethod> paymentList) {
        List<PaymentMethod> paymentTree = buildPaymentTree(paymentList);
        return paymentTree.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    private void recursionFn(List<PaymentMethod> paymentList , PaymentMethod payment){
        // 得到子节点列表
        List<PaymentMethod> childList = getChildList(paymentList,payment);
        payment.setChildrenPaymen(childList);
        for (PaymentMethod newPayment : childList)
        {
            if (hasChild(childList, newPayment))
            {
                recursionFn(paymentList, newPayment);
            }
        }
    }

    /**
     * 获得子节点
     * @param paymentList
     * @param payment
     * @return
     */
    private List<PaymentMethod> getChildList(List<PaymentMethod> paymentList, PaymentMethod payment){
        List<PaymentMethod> returnList = new ArrayList<>();
        Iterator<PaymentMethod> iterator = paymentList.iterator();
        while (iterator.hasNext()){
            PaymentMethod n = (PaymentMethod) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == payment.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    /**
     * 判断是否有子节点
     */
        private boolean hasChild(List<PaymentMethod> paymentList, PaymentMethod payment)
        {
            return getChildList(paymentList, payment).size() > 0;
        }

}
